package com.quectel.sdk.baidu.text;

import com.fasterxml.jackson.core.type.TypeReference;
import com.quectel.sdk.baidu.AbstractBaiduBCEBaseClient;
import com.quectel.sdk.baidu.model.text.BaiduBCETextResp;
import com.quectel.util.common.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/5/12 9:28
 * @description new java file header...
 */
@Slf4j
public class BaiduBCETextClient extends AbstractBaiduBCEBaseClient {

    public static final Integer MAX_TEXT_LENGTH = 6666;
    public static final Integer PASS_CHECK = 1;

    /**
     * 文本检测
     */
    private final static String TEXT_CENSOR_URI = "/rest/2.0/solution/v1/text_censor/v2/user_defined";


    public BaiduBCETextResp check(String text) {
        Map<String, Object> params = new HashMap<>();
        params.put("text", text);
        return requester.postForm(TEXT_CENSOR_URI, params, new TypeReference<BaiduBCETextResp>() {
        });
    }

    /**
     * 文本校验钱的检查
     * 文本长度限制：20000字节（约等于6666个字符数）
     *
     * @return
     */
    public Response requestPreCheck(String text) {
        if (StringUtils.isBlank(text)) {
            return Response.error("文本内容为空");
        }
        if (StringUtils.length(text) > MAX_TEXT_LENGTH) {
            return Response.error("文本长度超过限制");
        }
        return Response.ok();
    }
}
